import java.util.HashMap;
import java.util.Map;

public class Solution997 {

    public int findJudge(int n, int[][] trust) {


        //        Map<Integer, Integer> mapBeTrusted = new HashMap<>(2 * n);
        //        Map<Integer, Integer> mapVotes = new HashMap<>(2 * n);
        int[] beTrusted = new int[2 * n];
        int[] votes = new int[2 * n];

        for (int[] index : trust) {
            int a = index[0];
            votes[a]++;

            int b = index[1];
            beTrusted[b]++;

        }

        for (int i = 1; i <= n; i++) {
            if (beTrusted[i] == n - 1
                    && votes[i] == 0) {
                return i;
            }
        }

        return -1;
    }

    public static void main(String[] args) {

        System.out.println(new Solution997().findJudge(3, new int[][]{{1, 3}, {2, 3}}));
        System.out.println(new Solution997().findJudge(3, new int[][]{{1, 3}, {2, 3}, {3, 1}}));

    }

}
